﻿namespace MDXStudio
{
    using AdvancedDataGridView;
    using Microsoft.AnalysisServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    public class TraceWindow : DockContent
    {
        private IContainer components;
        private ImageList imageListTraceEvents;
        private SplitContainer splitContainer1;
        private TextBox textBoxFreeText;
        private DataGridViewTextBoxColumn TreeGridColumnDuration;
        private TreeGridColumn TreeGridColumnEvent;
        private DataGridViewTextBoxColumn TreeGridColumnRatioToParent;
        private DataGridViewTextBoxColumn TreeGridColumnRatioToTotal;
        private TreeGridView treeGridViewTrace;

        public TraceWindow()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(TraceWindow));
            this.treeGridViewTrace = new TreeGridView();
            this.TreeGridColumnEvent = new TreeGridColumn();
            this.TreeGridColumnDuration = new DataGridViewTextBoxColumn();
            this.TreeGridColumnRatioToTotal = new DataGridViewTextBoxColumn();
            this.TreeGridColumnRatioToParent = new DataGridViewTextBoxColumn();
            this.imageListTraceEvents = new ImageList(this.components);
            this.splitContainer1 = new SplitContainer();
            this.textBoxFreeText = new TextBox();
            ((ISupportInitialize) this.treeGridViewTrace).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            base.SuspendLayout();
            this.treeGridViewTrace.AllowUserToAddRows = false;
            this.treeGridViewTrace.AllowUserToDeleteRows = false;
            this.treeGridViewTrace.AllowUserToOrderColumns = true;
            this.treeGridViewTrace.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.treeGridViewTrace.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
            this.treeGridViewTrace.CellBorderStyle = DataGridViewCellBorderStyle.None;
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            style.BackColor = SystemColors.Control;
            style.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            style.ForeColor = SystemColors.WindowText;
            style.SelectionBackColor = SystemColors.Highlight;
            style.SelectionForeColor = SystemColors.HighlightText;
            style.WrapMode = DataGridViewTriState.True;
            this.treeGridViewTrace.ColumnHeadersDefaultCellStyle = style;
            this.treeGridViewTrace.Columns.AddRange(new DataGridViewColumn[] { this.TreeGridColumnEvent, this.TreeGridColumnDuration, this.TreeGridColumnRatioToTotal, this.TreeGridColumnRatioToParent });
            this.treeGridViewTrace.Dock = DockStyle.Fill;
            this.treeGridViewTrace.EditMode = DataGridViewEditMode.EditProgrammatically;
            this.treeGridViewTrace.ImageList = this.imageListTraceEvents;
            this.treeGridViewTrace.Location = new Point(0, 0);
            this.treeGridViewTrace.Name = "treeGridViewTrace";
            this.treeGridViewTrace.RowHeadersVisible = false;
            this.treeGridViewTrace.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.treeGridViewTrace.Size = new Size(0x1a2, 0x102);
            this.treeGridViewTrace.TabIndex = 1;
            this.treeGridViewTrace.CellContentClick += new DataGridViewCellEventHandler(this.treeGridViewTrace_CellContentClick);
            this.TreeGridColumnEvent.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.TreeGridColumnEvent.DefaultNodeImage = null;
            this.TreeGridColumnEvent.HeaderText = "Event";
            this.TreeGridColumnEvent.Name = "TreeGridColumnEvent";
            this.TreeGridColumnEvent.ReadOnly = true;
            this.TreeGridColumnEvent.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.TreeGridColumnDuration.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            this.TreeGridColumnDuration.FillWeight = 30f;
            this.TreeGridColumnDuration.HeaderText = "Duration";
            this.TreeGridColumnDuration.Name = "TreeGridColumnDuration";
            this.TreeGridColumnDuration.ReadOnly = true;
            this.TreeGridColumnDuration.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.TreeGridColumnDuration.Width = 0x36;
            this.TreeGridColumnRatioToTotal.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            this.TreeGridColumnRatioToTotal.FillWeight = 30f;
            this.TreeGridColumnRatioToTotal.HeaderText = "Ratio to Total";
            this.TreeGridColumnRatioToTotal.Name = "TreeGridColumnRatioToTotal";
            this.TreeGridColumnRatioToTotal.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.TreeGridColumnRatioToTotal.Width = 0x4e;
            this.TreeGridColumnRatioToParent.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            this.TreeGridColumnRatioToParent.FillWeight = 30f;
            this.TreeGridColumnRatioToParent.HeaderText = "Ratio to Parent";
            this.TreeGridColumnRatioToParent.Name = "TreeGridColumnRatioToParent";
            this.TreeGridColumnRatioToParent.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.TreeGridColumnRatioToParent.Width = 0x56;
            this.imageListTraceEvents.ImageStream = (ImageListStreamer) manager.GetObject("imageListTraceEvents.ImageStream");
            this.imageListTraceEvents.TransparentColor = Color.Transparent;
            this.imageListTraceEvents.Images.SetKeyName(0, "Run.ico");
            this.imageListTraceEvents.Images.SetKeyName(1, "MDXQuery.ico");
            this.imageListTraceEvents.Images.SetKeyName(2, "Partition.ico");
            this.imageListTraceEvents.Images.SetKeyName(3, "Cube.ico");
            this.imageListTraceEvents.Images.SetKeyName(4, "NamedSet.ico");
            this.imageListTraceEvents.Images.SetKeyName(5, "Script.ico");
            this.imageListTraceEvents.Images.SetKeyName(6, "Cache.ico");
            this.imageListTraceEvents.Images.SetKeyName(7, "Aggregate.ico");
            this.splitContainer1.Dock = DockStyle.Fill;
            this.splitContainer1.Location = new Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = Orientation.Horizontal;
            this.splitContainer1.Panel1.Controls.Add(this.treeGridViewTrace);
            this.splitContainer1.Panel2.Controls.Add(this.textBoxFreeText);
            this.splitContainer1.Size = new Size(0x1a2, 0x182);
            this.splitContainer1.SplitterDistance = 0x102;
            this.splitContainer1.TabIndex = 2;
            this.textBoxFreeText.AcceptsReturn = true;
            this.textBoxFreeText.Dock = DockStyle.Fill;
            this.textBoxFreeText.Font = new Font("Lucida Console", 9.75f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.textBoxFreeText.Location = new Point(0, 0);
            this.textBoxFreeText.Multiline = true;
            this.textBoxFreeText.Name = "textBoxFreeText";
            this.textBoxFreeText.ReadOnly = true;
            this.textBoxFreeText.ScrollBars = ScrollBars.Both;
            this.textBoxFreeText.Size = new Size(0x1a2, 0x7c);
            this.textBoxFreeText.TabIndex = 1;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x1a2, 0x182);
            base.Controls.Add(this.splitContainer1);
            base.HideOnClose = true;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "TraceWindow";
            base.TabText = "Trace";
            this.Text = "Trace";
            ((ISupportInitialize) this.treeGridViewTrace).EndInit();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.Panel2.PerformLayout();
            this.splitContainer1.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void treeGridViewTrace_CellContentClick(object sender, DataGridViewCellEventArgs de)
        {
            TreeGridView view = sender as TreeGridView;
            this.textBoxFreeText.Clear();
            List<DataGridViewRow> list = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in view.SelectedRows)
            {
                list.Add(row);
            }
            list.Sort(delegate (DataGridViewRow r1, DataGridViewRow r2) {
                return r1.Index - r2.Index;
            });
            Regex regex = new Regex(@"\[((\w)| |-|_)*\]:([^0\[]|(\[[^\]]*\]))");
            foreach (DataGridViewRow row2 in list)
            {
                TreeGridNode nodeForRow = view.GetNodeForRow(row2);
                if ((nodeForRow != null) && (nodeForRow.Tag != null))
                {
                    TraceEventArgs tag = nodeForRow.Tag as TraceEventArgs;
                    if (tag.get_TextData() != null)
                    {
                        string input = tag.get_TextData().Replace("\n", Environment.NewLine);
                        if (12 == tag.get_EventClass())
                        {
                            foreach (Match match in regex.Matches(input))
                            {
                                this.textBoxFreeText.AppendText(match.ToString());
                                this.textBoxFreeText.AppendText(Environment.NewLine);
                            }
                            this.textBoxFreeText.AppendText(Environment.NewLine);
                        }
                        this.textBoxFreeText.AppendText(input);
                        this.textBoxFreeText.AppendText(Environment.NewLine);
                        this.textBoxFreeText.AppendText(Environment.NewLine);
                    }
                }
            }
            this.textBoxFreeText.SelectionStart = 0;
            this.textBoxFreeText.ScrollToCaret();
        }

        internal TreeGridView TreeGridViewTrace
        {
            get
            {
                return this.treeGridViewTrace;
            }
        }
    }
}

